window.onload = function () {
        mui.init();
        //侧滑容器父节点
        var offCanvasWrapper = mui( '#offCanvasWrapper' );
        //主界面容器
        var offCanvasInner = offCanvasWrapper[ 0 ].querySelector( '.mui-inner-wrap' );
        //菜单容器
        var offCanvasSide = document.getElementById( "offCanvasSide" );
        //移动效果是否为整体移动
        var moveTogether = false;
        //侧滑容器的class列表，菜单和主界面一起移动；
        var classList = offCanvasWrapper[ 0 ].classList;
        //侧滑容器的class列表, 菜单移动、主界面不动
        //var classList = offCanvasWrapper[0].classList.add('mui-slide-in');

        //主界面和侧滑菜单界面均支持区域滚动；
        mui( '#offCanvasSideScroll' ).scroll();
        mui( '#offCanvasContentScroll' ).scroll();

        //绑定菜单栏点击事件
        mui( ".mui-bar-nav" ).on( "tap", "a", function () {
                if ( this.getAttribute( "id" ) == "menu" ) { //开关侧滑菜单
                        offCanvasWrapper.offCanvas( "show" );
                }
                if ( this.getAttribute( "id" ) == "title" ) { //更新书签列表
                        var angle = 0;
                        setInterval( function () {
                                angle += 3;
                                $( "#menu" ).rotate( angle );
                        }, 50 );
                        //getnewmarklist( Bookmarker, 0 );
                }
                if ( this.getAttribute( "id" ) == "scaner" ) { //通过二维码添加书籍
                        mui.openWindow( {
                                url: "barcode_scan.html",
                                id: "scanner"
                        } );
                }
        } );
}

mui.plusReady( function () {
        if ( plus.navigator.isImmersedStatusbar() ) { //判断设备是否支持沉浸式状态栏
                var ImmerseHeight = Math.round( plus.navigator.getStatusbarHeight() );
                document.body.setAttribute( "class", "mui-fullscreen" );
                document.body.style.top = ImmerseHeight + "px";
                document.body.style.height = document.body.offsetHeight + 'px';
        }
        document.querySelector( "#version" ).innerHTML = "Ver：" + plus.runtime.version;

        getRules();

} );

// 下载站点规则列表
var siteRules = [{}]; //网站规则容器
function getRules() {
        mui.toast( "加载站点规则列表" );
        mui.ajax( {
                url: "https://gitee.com/Antecer/H5Reader/raw/master/unpackage/webmarker.json",
                timeout: 5000,
                dataType: 'html',
                success: function ( rulesJSON ) {
                        siteRules = eval( rulesJSON );
                        if ( siteRules[ 0 ].网站名称 == 'default' ) siteRules.shift();
                        var webTab = $( $( '#weblist' ).html() );
                        $( '#weblist' ).empty();
                        $.each( siteRules, function ( n, item ) {
                                webTab.find( 'h4' ).html( item.网站名称 );
                                webTab.find( 'h6' ).html( item.网站首页 );
                                $( '#weblist' ).append( webTab.prop( 'outerHTML' ) );
                        } );
                        mui.toast( "规则列表载入完成" );
                },
                error: function ( e ) {
                        mui.toast( e.message );
                }
        } );
}

//自定义事件-添加书签
window.addEventListener( 'setbookmarker', function ( event ) {
        console.log('success');
        return;
        //获得事件参数 event.detail 是传递过来的变量名
        if ( typeof event.detail == "string" ) {
                addbookmark( event.detail );
        }
        if ( typeof event.detail == "object" ) {
                setbookmark( event.detail );
                if ( event.detail.title_last == "待更新" ) {
                        plus.nativeUI.showWaiting( "更新书签列表..." );
                        getnewmarklist( Bookmarker, 0 );
                }
        }
} );